package com.leetcode.threadtest;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/1/30 13:32
 * -@Content:
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
//        Executors.newCachedThreadPool();
//        Executors.newFixedThreadPool(5);
//        Executors.newSingleThreadExecutor();

        ThreadPoolExecutor poolExecutor =
                new ThreadPoolExecutor(2,//核心线程
                        5,// 最大线程
                        3,
                        TimeUnit.SECONDS,
                        new LinkedBlockingQueue<>(3),// 阻塞队列容量3
                        Executors.defaultThreadFactory(),
                        new ThreadPoolExecutor.CallerRunsPolicy()//当新的任务被拒绝时，会抛出一个异常，当线程数>最大线程数+阻塞队列容量,就会抛异常
                );

        try {
            for (int i = 1; i <=10 ; i++) {
                poolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" OK");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            poolExecutor.shutdown();//线程池用完，关闭线程池。
        }


    }
}
